{template 'common/header'}
<ul class="nav nav-tabs">
	<li {if $do == 'display'}class="active"{/if}><a href="{php echo url('activity/coupon/display', array());}">卡券列表</a></li>
	<li {if $do == 'post' && !$couponid}class="active"{/if}><a href="javascript:;" data-toggle="modal" data-target="#cardtype-modal">添加卡券</a></li>
	{if $do == 'post' && $couponid}<li class="active"><a href="{php echo url('activity/coupon/post', array('id' => $couponid))}">编辑卡券</a></li>{/if}
</ul>
<div class="modal fade" id="cardtype-modal" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">选择你要创建的卡券类型</h4>
			</div>
			<div class="modal-body clearfix form-horizontal">
				<div class="form-group marbot0">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"></label>
					<div class="col-sm-9 col-xs-12">
						<label class="radio-inline">
							<input type="radio" name="type" value="{COUPON_TYPE_DISCOUNT}"/> 折扣券
						</label>
						<div class="help-block">可为用户提供消费折扣</div>
					</div>
				</div>
				<div class="form-group marbot0">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"></label>
					<div class="col-sm-9 col-xs-12">
						<label class="radio-inline">
							<input type="radio" name="type" value="{COUPON_TYPE_CASH}"/> 代金券
						</label>
						<div class="help-block">可为用户提供抵扣现金服务。可设置成为“满*元，减*元”</div>
					</div>
				</div>
				{if COUPON_TYPE == WECHAT_COUPON}
				<div class="form-group marbot0">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"></label>
					<div class="col-sm-9 col-xs-12">
						<label class="radio-inline">
							<input type="radio" name="type" value="{COUPON_TYPE_GIFT}"/> 礼品券
						</label>
						<div class="help-block">可为用户提供消费送赠品服务</div>
					</div>
				</div>
				<div class="form-group marbot0">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"></label>
					<div class="col-sm-9 col-xs-12">
						<label class="radio-inline">
							<input type="radio" name="type" value="{COUPON_TYPE_GROUPON}"/> 团购券
						</label>
						<div class="help-block">可为用户提供团购套餐服务</div>
					</div>
				</div>
				<div class="form-group marbot0">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"></label>
					<div class="col-sm-9 col-xs-12">
						<label class="radio-inline">
							<input type="radio" name="type" value="{COUPON_TYPE_GENERAL}"/> 优惠券
						</label>
						<div class="help-block">即“通用券”，建议当以上四种无法满足需求时采用</div>
					</div>
				</div>
				{/if}
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="location.href='{php echo url('activity/coupon/post')}&type=' + $('#cardtype-modal input[type=radio]:checked').val()">确定</button>
			</div>
		</div>
	</div>
</div>
{if $_W['account']['level'] == ACCOUNT_SERVICE_VERIFY || $_W['account']['level'] == ACCOUNT_SUBSCRIPTION_VERIFY}
<div class="panel panel-default">
	<div class="panel-heading">
		是否使用系统卡券：
		<input type="checkbox" name="flag" value="0" {if intval($uni_setting['coupon_type']) == SYSTEM_COUPON} checked="checked" {/if}/>
	</div>
</div>
{/if}
<div class="main" id="coupon_display" ng-controller="CouponDisplayCtrl">
	<div class="panel panel-info">
	<div class="panel-heading">筛选</div>
	<div class="panel-body">
		<form action="./index.php" method="get" class="form-horizontal" role="form">
		<input type="hidden" name="c" value="activity" />
		<input type="hidden" name="a" value="coupon" />
		<input type="hidden" name="type" value="{$_GPC['type']}" />
			<div class="form-group">
				<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">卡券类型</label>
				<div class="col-sm-7 col-lg-9 col-xs-12 btn-group">
					<a href="{php echo filter_url('type:');}" class="btn {if $_GPC['type'] == ''}btn-primary{else}btn-default{/if}">不限</a>
					<a href="{php echo filter_url('type:'.COUPON_TYPE_DISCOUNT);}" class="btn {if $_GPC['type'] == COUPON_TYPE_DISCOUNT}btn-primary{else}btn-default{/if}">折扣券</a>
					<a href="{php echo filter_url('type:'.COUPON_TYPE_CASH);}" class="btn {if $_GPC['type'] == COUPON_TYPE_CASH}btn-primary{else}btn-default{/if}">代金券</a>
					{if COUPON_TYPE == WECHAT_COUPON}
					<a href="{php echo filter_url('type:'.COUPON_TYPE_GIFT);}" class="btn {if $_GPC['type'] == COUPON_TYPE_GIFT}btn-primary{else}btn-default{/if}">礼品券</a>
					<a href="{php echo filter_url('type:'.COUPON_TYPE_GROUPON);}" class="btn {if $_GPC['type'] == COUPON_TYPE_GROUPON}btn-primary{else}btn-default{/if}">团购券</a>
					<a href="{php echo filter_url('type:'.COUPON_TYPE_GENERAL);}" class="btn {if $_GPC['type'] == COUPON_TYPE_GENERAL}btn-primary{else}btn-default{/if}">优惠券</a>
					{/if}
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">审核状态</label>
				<div class="col-sm-7 col-lg-9 col-xs-12">
					<div class="btn-group">
						<a href="{php echo filter_url('status:');}" class="btn {if $_GPC['status'] == ''}btn-primary{else}btn-default{/if}">不限</a>
						<a href="{php echo filter_url('status:1');}" class="btn {if $_GPC['status'] == 1}btn-primary{else}btn-default{/if}">审核中</a>
						<a href="{php echo filter_url('status:2');}" class="btn {if $_GPC['status'] == 2}btn-primary{else}btn-default{/if}">未通过</a>
						<a href="{php echo filter_url('status:3');}" class="btn {if $_GPC['status'] == 3}btn-primary{else}btn-default{/if}">已通过</a>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">可用会员组</label>
				<div class="col-sm-7 col-lg-9 col-xs-12">
					<select name="groupid" class="form-control">
						{loop $groups $row}
							<option {if $_GPC['groupid'] == $row['id']}selected{/if} value="{$row['id']}">{$row['name']}</option>
						{/loop}
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">可用门店</label>
				<div class="col-sm-7 col-lg-9 col-xs-12">
					<select name="storeid" class="form-control">
						<option value="0">不限</option>
						{loop $location_store $row}
							<option {if $_GPC['storeid'] == $row['id']}selected{/if} value="{$row['id']}">{$row['business_name']}</option>
						{/loop}
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">可用模块</label>
				<div class="col-sm-7 col-lg-9 col-xs-12">
					<select name="moduleid" class="form-control">
						<option value="0">不限</option>
						{loop $module $row}
							{if empty($row['issystem'])}
							<option {if $_GPC['moduleid'] == $row['name']}selected{/if} value="{$row['name']}">{$row['title']}</option>
							{/if}
						{/loop}
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">券标题</label>
				<div class="col-sm-7 col-lg-9 col-xs-12">
					<input class="form-control" name="title" placeholder="券标题" type="text" value="{$_GPC['title']}">
				</div>
				<div class="pull-right col-xs-12 col-sm-3 col-lg-2">
					<button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
				</div>
			</div>
		</form>
	</div>
</div>
<div class="modal fade" id="quantity-modal" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title" id="myModalLabel">修改库存</h4>
			</div>
			<div class="modal-body clearfix form-horizontal">
				<input type="text" class="form-control" name="quantity" />
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="util.ajaxshow('{php echo url('activity/coupon/modifystock')}&id=&quantity=' + $('input[name=quantity]').val() + '&id='+window.couponid)">确定</button>
			</div>
		</div>
	</div>
</div>
<div class="panel panel-default">
	<div class="table-responsive panel-body">
		<table class="table table-hover">
			<thead class="navbar-inner">
				<tr>
					<th width="80px">卡券类型</th>
					<th width="120px">卡券名称</th>
					<th width="150px">卡券有效期</th>
					<th width="70px">状态</th>
					<th width="100px">库存</th>
					<th width="50px">限领</th>
					<th width="80px">上架状态</th>
					<th style="width:450px; text-align:right;">操作</th>
				</tr>
			</thead>
			<tbody>
				{loop $list $item}
				<tr>
					<td>{$item['type'][0]}</td>
					<td>{$item['title']}</td>
					<td>
						{$item['date_info']}
					</td>
					<td>
						{if $item['status'] == '1'}
						<span class="label label-info">审核中</span>
						{elseif $item['status'] == '2'}
						<span class="label label-danger">未通过</span>
						{elseif $item['status'] == '3'}
						<span class="label label-success">已通过</span>
						{elseif $item['status'] == '4'}
						<span class="label label-default">卡券被商户删除</span>
						{elseif $item['status'] == '5'}
						<span class="label label-warning">已在公众平台投放</span>
						{/if}
					</td>
					<td>{$item['quantity']}</td>
					<td>{$item['get_limit']}</td>
					<td>
						{if $item['is_display'] == 1}
							<span class="label label-success">上架中</span>
						{else}
							<span class="label label-danger">已下架</span>
						{/if}
					</td>
					<td style="text-align:right;">
						{if COUPON_TYPE == WECHAT_COUPON}<a href="javascript:;" onclick="util.ajaxshow('{php echo url('activity/coupon/selfconsume', array('id' => $item['id']))}')" class="btn {if $item['is_selfconsume'] == 1}btn-danger{else}btn-success{/if} btn-sm toggle-selfconsume" title="自助核销">{if $item['is_selfconsume'] == 1}关闭自助核销{else}开启自助核销{/if}</a>
						<a href="#put-coupon" data-toggle="modal" data-cid={$item['id']} class="btn btn-default btn-sm js-publish">投放</a>
						{/if}
						<a href="javascript:;" class="btn btn-default btn-sm" onclick="window.couponid = '{$item['id']}';$('input[name=quantity]').val('');" data-toggle="modal" data-target="#quantity-modal">修改库存</a>
						<a href="javascript:;" onclick="util.ajaxshow('{php echo url('activity/coupon/toggle', array('id' => $item['id']))}')" class="btn btn-default btn-sm toggle-display" title="上架/下架">{if $item['is_display'] == 1}下架{else}上架{/if}</a>
						<a href="{php echo url('activity/coupon/detail', array('id' => $item['id']))}" class="btn btn-success btn-sm" title="查看详情">查看详情</a>
						<a href="{php echo url('activity/consume/display', array('couponid' => $item['id'], 'status' => '3'))}"  class="btn btn-default btn-sm" title="领取记录">领取记录</a>
						<a href="{php echo url('activity/coupon/del', array('id' => $item['id']))}"  class="btn btn-default btn-sm" title="删除" onclick="if(!confirm('删除后将不可恢复，确定删除吗?')) return false;">删除</a>
					</td>
				</tr>
				{/loop}
			</tbody>
		</table>
	</div>
</div>
	{if COUPON_TYPE == WECHAT_COUPON}
	<a class="btn btn-success js-sync pull-left" onclick="util.ajaxshow('{php echo url('activity/coupon/sync', array('type' => 1))}')">更新全部卡券最新状态</a>&nbsp;&nbsp;
	<a class="btn btn-success" ng-click="download();">同步卡券信息</a>
	{/if}
	<div class="pull-right">{$pager}</div>
</div>
{if COUPON_TYPE == WECHAT_COUPON}
<div class="modal fade" id="put-coupon">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-body">
				<div class="form-group">
					<ul class="nav nav-tabs" role="tablist">
					<li role="presentation" class="active"><a href="#put-qrcode" aria-controls="qrcode" role="tab" data-toggle="tab">二维码</a></li>
					<li role="presentation"><a href="#put-coupon-record" aria-controls="put-coupon-record" role="tab" data-toggle="tab">领取记录</a></li>
				</ul>
				</div>
				<div class="tab-content">
					<div role="tabpanel" class="tab-pane active text-center" id="put-qrcode">
						<div class="alert alert-info text-left">
							该投放二维码有效期至：<span class="js-qrcode-expire"></span> <br />
							如果您要大量投放卡券，请使用卡券营销功能
						</div>
						<img src="" class="js-qrcode-src">
					</div>
					<div role="tabpanel" class="tab-pane" id="put-coupon-record">
						<div class="alert alert-info text-left">
							共有 <span class="js-qrcode-record-total"></span> 人领取
						</div>
						<table class="table table-hover">
							<thead class="table table-hover">
								<tr>
									<th style="width: 20%;"></th>
									<th>昵称</th>
									<th style="width: 30%;">领取时间</th>
								</tr>
							</thead>
							<tbody class="js-qrcode-record"></tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	$(document).on('click', '.js-publish', function() {
		cid = $(this).data('cid');
		$.post("{php echo url('activity/coupon/publish')}", {'cid' : cid}, function(data) {
			var data = $.parseJSON(data);
			if (data.message.errno == '0') {
				var coupon = data.message.message.coupon;
				$('.js-qrcode-src').attr('src', coupon.url);
				$('.js-qrcode-expire').html(coupon.expire);
				$('.js-qrcode-record-total').html(data.message.message.total);
				var record = data.message.message.record;
				if (record) {
					$('.js-qrcode-record').html('');
					for (i in record) {
						$('.js-qrcode-record').append('<tr><td><img src="'+record[i].avatar+'" width="50" /></td><td><a href="#">'+record[i].nickname+'</a></td><td><a href="#">'+record[i].createtime+'</a></td></tr>');
					}
				}
			} else {
				$('#put-coupon').modal('hide');
				util.message(data.message.message);
			}
		});
	});
	$(function(){
		angular.module('couponApp').value('config', {
			'download_url' : "{php echo url('activity/coupon/download')}",
			'success_url' : "{php echo url('activity/coupon/display')}"
		});
		angular.bootstrap(document, ['couponApp']);
	});
</script>
{/if}
<script>
	$(function(){
		require(['bootstrap.switch'], function() {
			$(":checkbox[name='flag']").bootstrapSwitch();
			$(':checkbox').on('switchChange.bootstrapSwitch', function(e, state){
				$this = $(this);
				var status = this.checked ? 1 : 2;
				var hint = status == 1 ? '切换为系统卡券将不显示微信卡券及微信门店' : '切换为微信卡券将不显示系统卡券及系统门店';
				if (confirm(hint) == false) {
						location.href = location.href;
					return false;
				}
				$.post("{php echo url('activity/coupon/exchange_coupon_type');}", {'status' : status}, function(resp){
					resp = $.parseJSON(resp);
					if (resp.message.errno < 0) {
						util.message(resp.message.message, location.href, 'error');
					} else {
						util.message(resp.message.message, location.href, 'success');
					}
				});
			});
		});
	});
</script>
{template 'common/footer'}